/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.dynread.com
 */
package com.dynread.web.controller.mall;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dynread.common.core.controller.BaseController;
import com.dynread.common.core.domain.AjaxResult;
import com.dynread.mall.entity.GoodsCategory;
import com.dynread.mall.service.GoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 商品分类
 *
 * @author drinvay
 * @date 2022/5/6 10:12
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/goodscategory")
@Api(value = "goodscategory", tags = "商品分类管理")
public class GoodsCategoryController extends BaseController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param goodsCategory 商品分类
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('mall:goodscategory:index')")
    public AjaxResult getGoodsCategoryPage(Page page, GoodsCategory goodsCategory) {
        return AjaxResult.success(goodsCategoryService.page(page, Wrappers.query(goodsCategory)));
    }

    /**
     * 返回树形集合
     *
     * @return
     */
    @ApiOperation(value = "返回树形集合")
    @GetMapping("/tree")
    @PreAuthorize("@ss.hasPermi('mall:goodscategory:index')")
    public AjaxResult getGoodsCategoryTree() {
        return AjaxResult.success(goodsCategoryService.selectTree(null));
    }

    /**
     * 通过id查询商品分类
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id查询商品分类")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('mall:goodscategory:get')")
    public AjaxResult getById(@PathVariable("id") String id) {
        return AjaxResult.success(goodsCategoryService.getById(id));
    }

    /**
     * 新增商品分类
     *
     * @param goodsCategory
     * @return com.dynread.common.core.domain.AjaxResult
     * @author drinvay
     * @date 2022/5/6 10:34
     */
    @ApiOperation(value = "新增商品分类")
    @PostMapping
    @PreAuthorize("@ss.hasPermi('mall:goodscategory:add')")
    public AjaxResult save(@RequestBody GoodsCategory goodsCategory) {
        return AjaxResult.success(goodsCategoryService.add(goodsCategory));
    }

    /**
     * 修改商品分类
     *
     * @param goodsCategory
     * @return com.dynread.common.core.domain.AjaxResult
     * @author drinvay
     * @date 2022/5/6 10:38
     */
    @ApiOperation(value = "修改商品分类")
    @PutMapping
    @PreAuthorize("@ss.hasPermi('mall:goodscategory:edit')")
    public AjaxResult updateById(@RequestBody GoodsCategory goodsCategory) {
        if (goodsCategory.getId().equals(goodsCategory.getParentId())) {
            return AjaxResult.error("不能将本级设为父类");
        }
        return AjaxResult.success(goodsCategoryService.modify(goodsCategory));
    }

    /**
     * 通过id删除商品分类
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id删除商品分类")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('mall:goodscategory:del')")
    public AjaxResult removeById(@PathVariable String id) {
        return AjaxResult.success(goodsCategoryService.removeById(id));
    }

}
